/*
 *------------------------------------------------------------------------------
 * @File      :    wks43178_cpu.c
 * @Date      :    2021-3-20
 * @Author    :    lomboswer <lomboswer@lombotech.com>
 * @Brief     :    Source file for MDP(Media Development Platform).
 *
 * Copyright (C) 2020-2021, LomboTech Co.Ltd. All rights reserved.
 *------------------------------------------------------------------------------
 */
#include "ei_comm_panel.h"
#include <fcntl.h>
#include "panel_usr.h"
#include "ei_math.h"

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif /* __cplusplus */

static VO_PUB_ATTR_S wks43178_config = {
    .enIntfType = VO_INTF_I8080_16BIT_RGB565,
    .enIntfSync = VO_OUTPUT_USER,
    .BufferSmooth = (-1U),
    .stSyncInfo.bHasHsyncCmd = 0,
    .stSyncInfo.bHasVsyncCmd = 0,
    .stSyncInfo.u32CmdAlign = 0,
    .stSyncInfo.bHasTE = 0,
    .stSyncInfo.bTEPol = 1,

    .stSyncInfo.bIop = 1,
    .stSyncInfo.u16Hfb = 40,
    .stSyncInfo.u16Hpw = 10,
    .stSyncInfo.u16Hbb = 20,
    .stSyncInfo.u16Hact = 800,
    .stSyncInfo.u16Vfb = 15,
    .stSyncInfo.u16Vpw = 10,
    .stSyncInfo.u16Vbb = 10,
    .stSyncInfo.u16Vact = 480,
    .stSyncInfo.u32Pclk = 27000, /* 27 MHz, if on fpga, should be 30 MHz */
};

unsigned int panel_kws43178_init_code[] = {
    /********* Start Initial Sequence ******/
    /**********LV2 Page 1 enable************/
    2, 0xF000, 0x0055,
    2, 0xF001, 0x00AA,
    2, 0xF002, 0x0052,
    2, 0xF003, 0x0008,
    2, 0xF004, 0x0001,

    /************AVDD Set AVDD 5.2V*********/
    2, 0xB000, 0x000D,
    2, 0xB001, 0x000D,
    2, 0xB002, 0x000D,

    /***********AVDD ratio******************/
    2, 0xB600, 0x0034,
    2, 0xB601, 0x0034,
    2, 0xB602, 0x0034,

    /***********AVEE  -5.2V*****************/
    2, 0xB100, 0x000D,
    2, 0xB101, 0x000D,
    2, 0xB102, 0x000D,

    /**************AVEE ratio***************/
    2, 0xB700, 0x0034,
    2, 0xB701, 0x0034,
    2, 0xB702, 0x0034,

    /*************VCL  -2.5V*****************/
    2, 0xB200, 0x0000,
    2, 0xB201, 0x0000,
    2, 0xB202, 0x0000,

    /*************VCL ratio******************/
    2, 0xB800, 0x0024,
    2, 0xB801, 0x0024,
    2, 0xB802, 0x0024,

    /*************VGH 15V  (Free pump)********/
    2, 0xBF00, 0x0001,
    2, 0xB300, 0x000F,
    2, 0xB301, 0x000F,
    2, 0xB302, 0x000F,

    /**************VGH ratio******************/
    2, 0xB900, 0x0034,
    2, 0xB901, 0x0034,
    2, 0xB902, 0x0034,

    /**************VGL_REG -10V***************/
    2, 0xB500, 0x0008,
    2, 0xB501, 0x0008,
    2, 0xB502, 0x0008,

    2, 0xC200, 0x0003,

    /****************VGLX ratio***************/
    2, 0xBA00, 0x0024,
    2, 0xBA01, 0x0024,
    2, 0xBA02, 0x0024,

    /************VGMP/VGSP 4.5V/0V************/
    2, 0xBC00, 0x0000,
    2, 0xBC01, 0x0078,
    2, 0xBC02, 0x0000,

    /***********VGMN/VGSN -4.5V/0V************/
    2, 0xBD00, 0x0000,
    2, 0xBD01, 0x0078,
    2, 0xBD02, 0x0000,

    /***********VCOM  -1.25V******************/
    2, 0xBE00, 0x0000,
    2, 0xBE01, 0x0067,

    /***********Gamma Setting*****************/
    2, 0xD100, 0x0000,
    2, 0xD101, 0x0032,
    2, 0xD102, 0x0000,
    2, 0xD103, 0x0033,
    2, 0xD104, 0x0000,
    2, 0xD105, 0x0041,
    2, 0xD106, 0x0000,
    2, 0xD107, 0x005A,
    2, 0xD108, 0x0000,
    2, 0xD109, 0x0076,
    2, 0xD10A, 0x0000,
    2, 0xD10B, 0x00A7,
    2, 0xD10C, 0x0000,
    2, 0xD10D, 0x00CF,
    2, 0xD10E, 0x0001,
    2, 0xD10F, 0x0009,
    2, 0xD110, 0x0001,
    2, 0xD111, 0x0036,
    2, 0xD112, 0x0001,
    2, 0xD113, 0x0073,
    2, 0xD114, 0x0001,
    2, 0xD115, 0x009F,
    2, 0xD116, 0x0001,
    2, 0xD117, 0x00DF,
    2, 0xD118, 0x0002,
    2, 0xD119, 0x0010,
    2, 0xD11A, 0x0002,
    2, 0xD11B, 0x0011,
    2, 0xD11C, 0x0002,
    2, 0xD11D, 0x003D,
    2, 0xD11E, 0x0002,
    2, 0xD11F, 0x0069,
    2, 0xD120, 0x0002,
    2, 0xD121, 0x0081,
    2, 0xD122, 0x0002,
    2, 0xD123, 0x009D,
    2, 0xD124, 0x0002,
    2, 0xD125, 0x00AD,
    2, 0xD126, 0x0002,
    2, 0xD127, 0x00C3,
    2, 0xD128, 0x0002,
    2, 0xD129, 0x00D0,
    2, 0xD12A, 0x0002,
    2, 0xD12B, 0x00E2,
    2, 0xD12C, 0x0002,
    2, 0xD12D, 0x00EE,
    2, 0xD12E, 0x0003,
    2, 0xD12F, 0x0001,
    2, 0xD130, 0x0003,
    2, 0xD131, 0x0026,
    2, 0xD132, 0x0003,
    2, 0xD133, 0x008E,

    2, 0xD200, 0x0000,
    2, 0xD201, 0x0032,
    2, 0xD202, 0x0000,
    2, 0xD203, 0x0033,
    2, 0xD204, 0x0000,
    2, 0xD205, 0x0041,
    2, 0xD206, 0x0000,
    2, 0xD207, 0x005A,
    2, 0xD208, 0x0000,
    2, 0xD209, 0x0076,
    2, 0xD20A, 0x0000,
    2, 0xD20B, 0x00A7,
    2, 0xD20C, 0x0000,
    2, 0xD20D, 0x00CF,
    2, 0xD20E, 0x0001,
    2, 0xD20F, 0x0009,
    2, 0xD210, 0x0001,
    2, 0xD211, 0x0036,
    2, 0xD212, 0x0001,
    2, 0xD213, 0x0073,
    2, 0xD214, 0x0001,
    2, 0xD215, 0x009F,
    2, 0xD216, 0x0001,
    2, 0xD217, 0x00DF,
    2, 0xD218, 0x0002,
    2, 0xD219, 0x0010,
    2, 0xD21A, 0x0002,
    2, 0xD21B, 0x0011,
    2, 0xD21C, 0x0002,
    2, 0xD21D, 0x003D,
    2, 0xD21E, 0x0002,
    2, 0xD21F, 0x0069,
    2, 0xD220, 0x0002,
    2, 0xD221, 0x0081,
    2, 0xD222, 0x0002,
    2, 0xD223, 0x009D,
    2, 0xD224, 0x0002,
    2, 0xD225, 0x00AD,
    2, 0xD226, 0x0002,
    2, 0xD227, 0x00C3,
    2, 0xD228, 0x0002,
    2, 0xD229, 0x00D0,
    2, 0xD22A, 0x0002,
    2, 0xD22B, 0x00E2,
    2, 0xD22C, 0x0002,
    2, 0xD22D, 0x00EE,
    2, 0xD22E, 0x0003,
    2, 0xD22F, 0x0001,
    2, 0xD230, 0x0003,
    2, 0xD231, 0x0026,
    2, 0xD232, 0x0003,
    2, 0xD233, 0x008E,

    2, 0xD300, 0x0000,
    2, 0xD301, 0x0032,
    2, 0xD302, 0x0000,
    2, 0xD303, 0x0033,
    2, 0xD304, 0x0000,
    2, 0xD305, 0x0041,
    2, 0xD306, 0x0000,
    2, 0xD307, 0x005A,
    2, 0xD308, 0x0000,
    2, 0xD309, 0x0076,
    2, 0xD30A, 0x0000,
    2, 0xD30B, 0x00A7,
    2, 0xD30C, 0x0000,
    2, 0xD30D, 0x00CF,
    2, 0xD30E, 0x0001,
    2, 0xD30F, 0x0009,
    2, 0xD310, 0x0001,
    2, 0xD311, 0x0036,
    2, 0xD312, 0x0001,
    2, 0xD313, 0x0073,
    2, 0xD314, 0x0001,
    2, 0xD315, 0x009F,
    2, 0xD316, 0x0001,
    2, 0xD317, 0x00DF,
    2, 0xD318, 0x0002,
    2, 0xD319, 0x0010,
    2, 0xD31A, 0x0002,
    2, 0xD31B, 0x0011,
    2, 0xD31C, 0x0002,
    2, 0xD31D, 0x003D,
    2, 0xD31E, 0x0002,
    2, 0xD31F, 0x0069,
    2, 0xD320, 0x0002,
    2, 0xD321, 0x0081,
    2, 0xD322, 0x0002,
    2, 0xD323, 0x009D,
    2, 0xD324, 0x0002,
    2, 0xD325, 0x00AD,
    2, 0xD326, 0x0002,
    2, 0xD327, 0x00C3,
    2, 0xD328, 0x0002,
    2, 0xD329, 0x00D0,
    2, 0xD32A, 0x0002,
    2, 0xD32B, 0x00E2,
    2, 0xD32C, 0x0002,
    2, 0xD32D, 0x00EE,
    2, 0xD32E, 0x0003,
    2, 0xD32F, 0x0001,
    2, 0xD330, 0x0003,
    2, 0xD331, 0x0026,
    2, 0xD332, 0x0003,
    2, 0xD333, 0x008E,

    2, 0xD400, 0x0000,
    2, 0xD401, 0x0032,
    2, 0xD402, 0x0000,
    2, 0xD403, 0x0033,
    2, 0xD404, 0x0000,
    2, 0xD405, 0x0041,
    2, 0xD406, 0x0000,
    2, 0xD407, 0x005A,
    2, 0xD408, 0x0000,
    2, 0xD409, 0x0076,
    2, 0xD40A, 0x0000,
    2, 0xD40B, 0x00A7,
    2, 0xD40C, 0x0000,
    2, 0xD40D, 0x00CF,
    2, 0xD40E, 0x0001,
    2, 0xD40F, 0x0009,
    2, 0xD410, 0x0001,
    2, 0xD411, 0x0036,
    2, 0xD412, 0x0001,
    2, 0xD413, 0x0073,
    2, 0xD414, 0x0001,
    2, 0xD415, 0x009F,
    2, 0xD416, 0x0001,
    2, 0xD417, 0x00DF,
    2, 0xD418, 0x0002,
    2, 0xD419, 0x0010,
    2, 0xD41A, 0x0002,
    2, 0xD41B, 0x0011,
    2, 0xD41C, 0x0002,
    2, 0xD41D, 0x003D,
    2, 0xD41E, 0x0002,
    2, 0xD41F, 0x0069,
    2, 0xD420, 0x0002,
    2, 0xD421, 0x0081,
    2, 0xD422, 0x0002,
    2, 0xD423, 0x009D,
    2, 0xD424, 0x0002,
    2, 0xD425, 0x00AD,
    2, 0xD426, 0x0002,
    2, 0xD427, 0x00C3,
    2, 0xD428, 0x0002,
    2, 0xD429, 0x00D0,
    2, 0xD42A, 0x0002,
    2, 0xD42B, 0x00E2,
    2, 0xD42C, 0x0002,
    2, 0xD42D, 0x00EE,
    2, 0xD42E, 0x0003,
    2, 0xD42F, 0x0001,
    2, 0xD430, 0x0003,
    2, 0xD431, 0x0026,
    2, 0xD432, 0x0003,
    2, 0xD433, 0x008E,

    2, 0xD500, 0x0000,
    2, 0xD501, 0x0032,
    2, 0xD502, 0x0000,
    2, 0xD503, 0x0033,
    2, 0xD504, 0x0000,
    2, 0xD505, 0x0041,
    2, 0xD506, 0x0000,
    2, 0xD507, 0x005A,
    2, 0xD508, 0x0000,
    2, 0xD509, 0x0076,
    2, 0xD50A, 0x0000,
    2, 0xD50B, 0x00A7,
    2, 0xD50C, 0x0000,
    2, 0xD50D, 0x00CF,
    2, 0xD50E, 0x0001,
    2, 0xD50F, 0x0009,
    2, 0xD510, 0x0001,
    2, 0xD511, 0x0036,
    2, 0xD512, 0x0001,
    2, 0xD513, 0x0073,
    2, 0xD514, 0x0001,
    2, 0xD515, 0x009F,
    2, 0xD516, 0x0001,
    2, 0xD517, 0x00DF,
    2, 0xD518, 0x0002,
    2, 0xD519, 0x0010,
    2, 0xD51A, 0x0002,
    2, 0xD51B, 0x0011,
    2, 0xD51C, 0x0002,
    2, 0xD51D, 0x003D,
    2, 0xD51E, 0x0002,
    2, 0xD51F, 0x0069,
    2, 0xD520, 0x0002,
    2, 0xD521, 0x0081,
    2, 0xD522, 0x0002,
    2, 0xD523, 0x009D,
    2, 0xD524, 0x0002,
    2, 0xD525, 0x00AD,
    2, 0xD526, 0x0002,
    2, 0xD527, 0x00C3,
    2, 0xD528, 0x0002,
    2, 0xD529, 0x00D0,
    2, 0xD52A, 0x0002,
    2, 0xD52B, 0x00E2,
    2, 0xD52C, 0x0002,
    2, 0xD52D, 0x00EE,
    2, 0xD52E, 0x0003,
    2, 0xD52F, 0x0001,
    2, 0xD530, 0x0003,
    2, 0xD531, 0x0026,
    2, 0xD532, 0x0003,
    2, 0xD533, 0x008E,

    2, 0xD600, 0x0000,
    2, 0xD601, 0x0032,
    2, 0xD602, 0x0000,
    2, 0xD603, 0x0033,
    2, 0xD604, 0x0000,
    2, 0xD605, 0x0041,
    2, 0xD606, 0x0000,
    2, 0xD607, 0x005A,
    2, 0xD608, 0x0000,
    2, 0xD609, 0x0076,
    2, 0xD60A, 0x0000,
    2, 0xD60B, 0x00A7,
    2, 0xD60C, 0x0000,
    2, 0xD60D, 0x00CF,
    2, 0xD60E, 0x0001,
    2, 0xD60F, 0x0009,
    2, 0xD610, 0x0001,
    2, 0xD611, 0x0036,
    2, 0xD612, 0x0001,
    2, 0xD613, 0x0073,
    2, 0xD614, 0x0001,
    2, 0xD615, 0x009F,
    2, 0xD616, 0x0001,
    2, 0xD617, 0x00DF,
    2, 0xD618, 0x0002,
    2, 0xD619, 0x0010,
    2, 0xD61A, 0x0002,
    2, 0xD61B, 0x0011,
    2, 0xD61C, 0x0002,
    2, 0xD61D, 0x003D,
    2, 0xD61E, 0x0002,
    2, 0xD61F, 0x0069,
    2, 0xD620, 0x0002,
    2, 0xD621, 0x0081,
    2, 0xD622, 0x0002,
    2, 0xD623, 0x009D,
    2, 0xD624, 0x0002,
    2, 0xD625, 0x00AD,
    2, 0xD626, 0x0002,
    2, 0xD627, 0x00C3,
    2, 0xD628, 0x0002,
    2, 0xD629, 0x00D0,
    2, 0xD62A, 0x0002,
    2, 0xD62B, 0x00E2,
    2, 0xD62C, 0x0002,
    2, 0xD62D, 0x00EE,
    2, 0xD62E, 0x0003,
    2, 0xD62F, 0x0001,
    2, 0xD630, 0x0003,
    2, 0xD631, 0x0026,
    2, 0xD632, 0x0003,
    2, 0xD633, 0x008E,

    /**************LV2 Page 0 enable********/
    2, 0xF000, 0x0055,
    2, 0xF001, 0x00AA,
    2, 0xF002, 0x0052,
    2, 0xF003, 0x0008,
    2, 0xF004, 0x0000,

    /************480x800********************/
    2, 0xB500, 0x0050,

    /************Display control************/
    2, 0xB100, 0x00CC,
    2, 0xB101, 0x0000,

    /************Source hold time***********/
    2, 0xB600, 0x0005,

    /***********Gate EQ control*************/
    2, 0xB700, 0x0070,
    2, 0xB701, 0x0070,

    /***********Source EQ control (Mode 2)**/
    2, 0xB800, 0x0001,
    2, 0xB801, 0x0003,
    2, 0xB802, 0x0003,
    2, 0xB803, 0x0003,

    /**********Inversion mode  (2-dot)******/
    2, 0xBC00, 0x0002,
    2, 0xBC01, 0x0000,
    2, 0xBC02, 0x0000,

    /************Frame rate*****************/
    2, 0xBD00, 0x0001,
    2, 0xBD01, 0x0084,
    2, 0xBD02, 0x001C,
    2, 0xBD03, 0x001C,
    2, 0xBD04, 0x0000,

    /*****Timng control 4H w/ 4-Delayms ****/
    2, 0xC900, 0x00D0,
    2, 0xC901, 0x0002,
    2, 0xC902, 0x0050,
    2, 0xC903, 0x0050,
    2, 0xC904, 0x0050,

    2, 0x3600, 0x0000,
    2, 0x3500, 0x0000,

    2, 0x3A00, 0x0005,

    2, 0X3600, 0X60,
    2, 0X2A02, 0X03,
    2, 0X2A03, 0X1F,
    2, 0X2B02, 0X01,
    2, 0X2B03, 0XDF,

    1, 0x1100,
    0, 120, /* Delay 120ms */
    1, 0x2900,
    0, 50, /* Delay 50ms */
    1, 0x2C00,
};

EI_S32 panel_wks43178_get_panel_attr(PANEL_TYPE_E enPanelType, PANEL_TYPE_ATTR_S *pstPanelAttr)
{
    pstPanelAttr->stVoPubAttr = wks43178_config;
    return EI_SUCCESS;
}

EI_S32 panel_wks43178_get_panel_init_code(PANEL_TYPE_E enPanelType, PANEL_INIT_CODE_TYPE *init, EI_U32 *pLen)
{
    *init = (PANEL_INIT_CODE_TYPE)&panel_kws43178_init_code[0];
    *pLen = EI_ARRAY_SIZE(panel_kws43178_init_code);
    return EI_SUCCESS;
}

PANEL_OBJ_S stPanelWks43178Obj = {
    .pfnGetPanelAttrByPanel = panel_wks43178_get_panel_attr,
    .pfnGetPanelInitCodeByPanel = panel_wks43178_get_panel_init_code,
};

#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */

